package com.android.dx.command.dexer;

import com.android.dx.command.DxConsole;
import com.android.dx.command.UsageException;
import com.android.dx.dex.cf.CfOptions;

/**
 * Created by IntelliJ IDEA.
 * User: vhly
 * Date: 2009-10-13
 * Time: 17:57:31
 *
 * @author vhly[FR]
 */
public class Arguments {
    public boolean debug;
    public boolean verbose;
    public boolean verboseDump;
    public boolean coreLibrary;
    public String methodToDump;
    public int dumpWidth;
    public String outName;
    public String humanOutName;
    public boolean strictNameCheck;
    public boolean emptyOk;
    public boolean jarOutput;
    public boolean keepClassesInJar;
    public int positionInfo;
    public boolean localInfo;
    public String fileNames[];
    public boolean optimize;
    public String optimizeListFile;
    public String dontOptimizeListFile;
    public boolean statistics;
    public CfOptions cfOptions;

    public void parse(String args[]) {
        int at;
        for (at = 0; at < args.length; at++) {
            String arg = args[at];
            if (arg.equals("--") || !arg.startsWith("--"))
                break;
            if (arg.equals("--debug")) {
                debug = true;
                continue;
            }
            if (arg.equals("--verbose")) {
                verbose = true;
                continue;
            }
            if (arg.equals("--verbose-dump")) {
                verboseDump = true;
                continue;
            }
            if (arg.equals("--no-files")) {
                emptyOk = true;
                continue;
            }
            if (arg.equals("--no-optimize")) {
                optimize = false;
                continue;
            }
            if (arg.equals("--no-strict")) {
                strictNameCheck = false;
                continue;
            }
            if (arg.equals("--core-library")) {
                coreLibrary = true;
                continue;
            }
            if (arg.equals("--statistics")) {
                statistics = true;
                continue;
            }
            if (arg.startsWith("--optimize-list=")) {
                if (dontOptimizeListFile != null) {
                    System.err.println("--optimize-list and --no-optimize-list are incompatible.");
                    throw new UsageException();
                }
                optimize = true;
                optimizeListFile = arg.substring(arg.indexOf('=') + 1);
                continue;
            }
            if (arg.startsWith("--no-optimize-list=")) {
                if (dontOptimizeListFile != null) {
                    System.err.println("--optimize-list and --no-optimize-list are incompatible.");
                    throw new UsageException();
                }
                optimize = true;
                dontOptimizeListFile = arg.substring(arg.indexOf('=') + 1);
                continue;
            }
            if (arg.equals("--keep-classes")) {
                keepClassesInJar = true;
                continue;
            }
            if (arg.startsWith("--output=")) {
                outName = arg.substring(arg.indexOf('=') + 1);
                if (outName.endsWith(".zip") || outName.endsWith(".jar") || outName.endsWith(".apk")) {
                    jarOutput = true;
                    continue;
                }
                if (outName.endsWith(".dex") || outName.equals("-")) {
                    jarOutput = false;
                } else {
                    System.err.println((new StringBuilder()).append("unknown output extension: ").append(outName).toString());
                    throw new UsageException();
                }
                continue;
            }
            if (arg.startsWith("--dump-to=")) {
                humanOutName = arg.substring(arg.indexOf('=') + 1);
                continue;
            }
            if (arg.startsWith("--dump-width=")) {
                arg = arg.substring(arg.indexOf('=') + 1);
                dumpWidth = Integer.parseInt(arg);
                continue;
            }
            if (arg.startsWith("--dump-method=")) {
                methodToDump = arg.substring(arg.indexOf('=') + 1);
                jarOutput = false;
                continue;
            }
            if (arg.startsWith("--positions=")) {
                String pstr = arg.substring(arg.indexOf('=') + 1).intern();
                if ("none".equals(pstr)) {
                    positionInfo = 1;
                    continue;
                }
                if ("important".equals(pstr)) {
                    positionInfo = 3;
                    continue;
                }
                if ("lines".equals(pstr)) {
                    positionInfo = 2;
                } else {
                    System.err.println((new StringBuilder()).append("unknown positions option: ").append(pstr).toString());
                    throw new UsageException();
                }
                continue;
            }
            if (arg.equals("--no-locals")) {
                localInfo = false;
            } else {
                System.err.println((new StringBuilder()).append("unknown option: ").append(arg).toString());
                throw new UsageException();
            }
        }

        int fileCount = args.length - at;
        fileNames = new String[fileCount];
        System.arraycopy(args, at, fileNames, 0, fileCount);
        if (fileCount == 0) {
            if (!emptyOk) {
                System.err.println("no input files specified");
                throw new UsageException();
            }
        } else if (emptyOk) {
            System.out.println("ignoring input files");
            at = args.length;
        }
        if (humanOutName == null && methodToDump != null)
            humanOutName = "-";
        makeCfOptions();
    }

    void makeCfOptions() {
        cfOptions = new CfOptions();
        cfOptions.positionInfo = positionInfo;
        cfOptions.localInfo = localInfo;
        cfOptions.strictNameCheck = strictNameCheck;
        cfOptions.optimize = optimize;
        cfOptions.optimizeListFile = optimizeListFile;
        cfOptions.dontOptimizeListFile = dontOptimizeListFile;
        cfOptions.statistics = statistics;
        cfOptions.warn = DxConsole.err;
    }


    public Arguments() {
        debug = false;
        verbose = false;
        verboseDump = false;
        coreLibrary = false;
        methodToDump = null;
        dumpWidth = 0;
        outName = null;
        humanOutName = null;
        strictNameCheck = true;
        emptyOk = false;
        jarOutput = false;
        keepClassesInJar = false;
        positionInfo = 2;
        localInfo = true;
        optimize = true;
        optimizeListFile = null;
        dontOptimizeListFile = null;
    }
}
